package com.example.sys.controller;

import com.example.common.result.PageResult;
import com.example.sys.entity.dto.UserDto;
import com.example.sys.entity.query.UserQuery;
import com.example.sys.entity.vo.SysUserInfoVo;
import com.example.sys.service.UserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("user")
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping
    public String insertUser(@RequestBody @Validated(UserDto.insert.class) UserDto userDto) {
        userService.insertUser(userDto);
        return "success";
    }

    @DeleteMapping("/{userId}")
    public String deleteSysUserByUserId(@PathVariable("userId")Long userId) {
        userService.deleteSysUserByUserId(userId);
        return "success";
    }

    @PutMapping("/{userId}")
    public String updateSysUserByUserId(@PathVariable("userId")Long userId,@RequestBody @Validated(UserDto.update.class) UserDto userDto){
        userDto.setUserId(userId);
        userService.updateSysUserByUserId(userDto);
        return "success";
    }

    @GetMapping("/{userId}")
    public SysUserInfoVo selectSysUserByUserId(@PathVariable("userId")Long userId) {
        SysUserInfoVo vo = userService.selectSysUserByUserId(userId);
        return vo;
    }

    @GetMapping("/user-list")
    public PageResult<SysUserInfoVo> selectSysUserList(UserQuery userQuery) {
        return userService.selectSysUserList(userQuery);
    }
}